/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

package fr.univnantes.alma.arbres;

import java.util.Iterator;
import java.util.LinkedList;
import java.util.NoSuchElementException;

/**
 *
 * @author judu
 */
class ArbreLAIteratorDFS<T> implements Iterator<Noeud<T>> {

    private LinkedList<NoeudLA<T>> enVisite;

    ArbreLAIteratorDFS(NoeudLA<T> racine) {
        this.enVisite = new LinkedList<NoeudLA<T>>();
        this.enVisite.addLast(racine);
    }

    public boolean hasNext() {
        return !(this.enVisite.isEmpty());
    }


    private void setUpNext(NoeudLA<T> current){
        LinkedList<NoeudLA<T>> tmpLi = new LinkedList<NoeudLA<T>>();
        if(current.getFils() != null){
            Iterator<NoeudLA<T>> it = current.getFils().iterator();
            while(it.hasNext()){
                tmpLi.addFirst(it.next());
            }
        }
        this.enVisite.addAll(tmpLi);
    }


    public Noeud<T> next() {
        if(!this.hasNext()) throw new NoSuchElementException("Terminus de l'arbre, vérifiez en sortant que vous n'oubliez aucun bagage.");
        NoeudLA<T> retour = this.enVisite.removeLast();
        this.setUpNext(retour);
        return retour.autoCast();
    }

    public void remove() {
        throw new UnsupportedOperationException("Pas de ça chez nous");
    }

}
